#include "functions.h"




int main(void) {

	//hours and minutes for setting of the time (filled by command-line inputs
	int onhoura, onhourb, offhoura, offhourb, onminutea, onminuteb, offminutea, offminuteb;


	//Asking for the ontime of Switch a
	printf("enter the Time, you want to turn switch a on: (Format: HH:MM)>\n");
	scanf("%d:%d", &onhoura, &onminutea);

	//Asking for the offtime of Switch a
	printf("enter the Time, you want to turn switch a off: (Format: HH:MM)>\n");
	scanf("%d:%d", &offhoura, &offminutea);

	//Asking for the ontime of Switch b
	printf("enter the Time, you want to turn switch b on: (Format: HH:MM)>\n");
	scanf("%d:%d", &onhourb, &onminuteb);

	//Asking for the offtime of Switch b
	printf("enter the Time, you want to turn switch a off: (Format: HH:MM)>\n");
	scanf("%d:%d", &offhourb, &offminuteb);



	//Allocating storage for both switches (own struct as defined in functions.h)
	Switch* a = malloc(sizeof(Switch));
	Switch* b = malloc(sizeof(Switch));

	//"boolean" to determine, if a switch is allready turned on
	a->on = 0;
	b->on = 0;

	//Setting on and offtime for Switch a (for function definition, look @ switches.c
	int upa = setOntime(a, onhoura, onminutea);
	int downa = setDowntime(a, offhoura, offminutea);

	//Setting on and offtime for Switch b
	int upb = setOntime(b, onhourb,onminuteb);
	int downb = setDowntime(b,offhourb,offminuteb);



	//if all Times for every Switch were set correctly, the program starts its normal routine
	if(upa==0&&upb==0&&downa==0&&downb==0){

		//here I want to make some kind of keyboard-Interrupt in the future
		while(1==1){


			//if onDiff of switch a returns 0 (look at comparetime.c), and the switch is not allready turned on
			//a signal is sent to turn the switch on
			if(onDiff(a)==0 && a->on == 0){

				//setting the "boolean to 1, so that the program does not send permanently during controll period
				a->on=1;


				//forking for the execv() of the program
				int pid = fork();


				//child does execv of the sending process
				if(pid == 0){

					char * const argv[5] = {"../raspberry-remote/send", "11111", "2", "1"};
					if(!execv("../raspberry-remote/send", argv)){
						perror("Problem with execv(): ");
						return 1;
					}

				}

				//parent waits till child has finished
				else if(pid >0){
					wait(NULL);
				}

				//error handling
				else{
					perror("Problem with forking: ");
				}

			}

			//if offDiff of switch a returns 0 (look at comparetime.c), and the switch is turned on
			//a signal is sent, to turn the switch off
			else if(offDiff(a)==0 && a->on == 1){

				//setting the boolean to 0, so that the program does not send permanently during controll period
				a->on=0;

				//forking for the execv() of the program
				int pid = fork();


				//child does execv of the sending process
				if(pid == 0){
					char * const argv[5] = {"../raspberry-remote/send", "11111", "2", "0"};
					if(!execv("../raspberry-remote/send", argv)){
						perror("Problem with execv(): ");
						return 1;
					};

				}

				//parent waits till child has finished
				else if(pid >0){
					wait(NULL);
				}

				//error handling
				else{
					perror("Problem with forking: ");

				}

			};


	//Same eventhandling as for switch a
			if(onDiff(b)==0 && b->on == 0) {
				b->on=1;

				//forking for the execv() of the program
				int pid = fork();



				//child does execv of the sending process
				if(pid == 0){

					char * const argv[5] = {"../raspberry-remote/send", "11111", "4", "1"};
					if (!execv("../raspberry-remote/send", argv)){

						perror("Problem with execv(): ");
						return 1;
					}


				}

				//parent waits till child has finished
				else if(pid >0){
					wait(NULL);
				}

				//error handling
				else{
					perror("Problem with forking: ");
				}

			}



			else if(offDiff(b)==0 && b->on == 1){
				b->on=0;
				//forking for the execv() of the program
				int pid = fork();

				//child does execv of the sending process
				if(pid == 0){

					char * const argv[5] = {"../raspberry-remote/send", "11111", "4", "0"};
					if (!execv("../raspberry-remote/send", argv)){
						perror("Problem with execv(): ");
						return 1;
					}


				}

				//parent waits till child has finished
				else if(pid >0){
					wait(NULL);
				}

				//error handling
				else{
					perror("Problem with forking: ");

				}
			}

		}
		return 0;
	}



//Errorhandling, if the User does not Enter up- or downtimes correctly.
	else if(upa>0){
		fprintf(stderr, "Problem with the Uptime of Switch a: Hour must be between 0 and 24\n");
		return 1;
	}
	else if(upa<0){
		fprintf(stderr, "Problem with the Uptime of Switch a: Minute must be between 0 and 60\n");
		return 1;
	}

	else if(downa>0){
		fprintf(stderr, "Problem with the Downtime of Switch a: Hour must be between 0 and 24\n");
		return 1;
	}

	else if(downa<0){
		fprintf(stderr, "Problem with the Downtime of Switch a: Minute must be between 0 and 60\n");
		return 1;
	}

	else if(upb>0){
		fprintf(stderr, "Problem with the Uptime of Switch b: Hour must be between 0 and 24\n");
		return 1;
	}

	else if(upb<0){
		fprintf(stderr, "Problem with the Uptime of Switch b: Minute must be between 0 and 60\n");
		return 1;
	}

	else if(downb>0){
		fprintf(stderr, "Problem with the Downtime of Switch b: Hour must be between 0 and 24\n");
		return 1;
	}

	else if(downb<0){
		fprintf(stderr, "Problem with the Downtime of Switch b: Minute must be between 0 and 60\n");
		return 1;
	}

}
